package com.kogi.androidpatt.gettingInput.timeAndDate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.os.CountDownTimer;



import com.kogi.patterns.R;

public class plusMinus extends Activity
{
	 private TextView mDateDisplay,mPickTime;
	 public static TextView deathLine;
	 private CountDownTimer countDownTimer;
	 private Button mPickDate;
	 private int mYear;
	 private int mMonth;
	 private int mDay;

	 private int mHour;
	 private int mMinute;
	 
	 static final int DATE_DIALOG_ID = 0;
	 static final int TIME_DIALOG_ID = 1;
	 private Calendar startDate,endDate;
	 SimpleDateFormat sdf;
	 
	 @Override
	 protected void onCreate(Bundle savedInstanceState) 
	 {
		 
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.getting_input_plusminus);
	     sdf = new SimpleDateFormat("MMMM-dd-yyyy");
	     endDate = Calendar.getInstance();
	     mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
	     endDate.set(2012,11, 21);
	     mPickDate = (Button) findViewById(R.id.pickDate);	
	     mPickTime = (Button) findViewById(R.id.pickTime);
	     mPickTime.setOnClickListener(new View.OnClickListener() 
	     {
	    	 public void onClick(View v)
	    	 {
	    		 showDialog(TIME_DIALOG_ID);
	         }
	     });
	     mPickDate.setOnClickListener(new View.OnClickListener()  //add a click listener to the button
	     {
	    	 public void onClick(View v) 
	         {
	    		 showDialog(DATE_DIALOG_ID);
	         }
	      });
	      deathLine = (TextView)findViewById(R.id.deathLine);
	      startDate = Calendar.getInstance();	      
	      mYear = startDate.get(Calendar.YEAR);
	      mMonth = startDate.get(Calendar.MONTH)+1;
	      mDay = startDate.get(Calendar.DAY_OF_MONTH);
	      mHour = startDate.get(Calendar.HOUR_OF_DAY);
	      mMinute = startDate.get(Calendar.MINUTE);	    
	      startDate.set(mYear, mMonth, mDay);
	      mPickTime.setText(new StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));
	      mPickDate.setText(sdf.format(startDate.getTime()));
	      startCountDown();
	 }
	    
	 @Override
	 protected Dialog onCreateDialog(int id) 
	 {
		 switch (id) 
		 {
	     	case DATE_DIALOG_ID:
	     		return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
	     	case TIME_DIALOG_ID:
	            return new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
	     }
	     return null;
	 }
	 
	 // updates the date we display in the TextView
	 private void updateDate() 
	 {
		 mPickDate.setText(sdf.format(startDate.getTime()));
		 startCountDown();
	 }
	    
	 // the callback received when the user "sets" the date in the dialog
	 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
	 {
		 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
	     {	 
			 mYear = year;
			 mMonth = monthOfYear;
			 mDay = dayOfMonth;
	         startDate.set(year, monthOfYear, dayOfMonth);
	         countDownTimer.cancel();
	         updateDate();
	     }
	 };
	 
	// the callback received when the user "sets" the time in the dialog
	 private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() 
	 {
		 public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
		 {
			 mHour = hourOfDay;
	         mMinute = minute;
	         countDownTimer.cancel();
	         updateTime();
	     }		 
	 };
	 
	// updates the time we display in the TextView
	 private void updateTime() 
	 {
	     mPickTime.setText(new StringBuilder().append(pad(mHour)).append(":").append(pad(mMinute)));
	     startCountDown();	     
	 }

	 private void startCountDown()
	 {
		 long value = getDifference(startDate, endDate)-mHour*3600000-mMinute*60000;
		 countDownTimer = new CountDownTimerClass(value, 1000);
	     countDownTimer.start();
	 }
	 
	 private static String pad(int c) 
	 {
	     if (c >= 10)
	         return String.valueOf(c);
	     else
	         return "0" + String.valueOf(c);
	 }
	 
	 public static long getDifference(Calendar a, Calendar b) 
	 {
		    return  b.getTimeInMillis() - a.getTimeInMillis();
	 }
	 
}
